﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class TechnologyRepository : RepositoryBase<Technology>
    {

        private readonly ProductRepository _productRepository = new ProductRepository();

        public IQueryable<ProductTechnologyView> GetList(int status, string productName, string productNum, string technologyNum, DateTime beginTime, DateTime endTime)
        {
            var result = from tech in dbcontext.Technologies
                         join orderpro in dbcontext.OrderProducts
                         on tech.OrderProductId equals orderpro.Id
                         join pro in dbcontext.Products
                         on orderpro.ProductId equals pro.Id
                         //join order in dbcontext.Orders
                         //on orderpro.OrderId equals order.Id
                         join u in dbcontext.Users
                         on tech.UserId equals u.Id
                         where (status == -10 || tech.Status == status)
                          && (orderpro.Status != -1)
                          && (tech.Number.Contains(technologyNum))
                          && (pro.Name.Contains(productName))
                          && (pro.Number.Contains(productNum))
                          && (tech.ApplyTime >= beginTime) && (tech.ApplyTime <= endTime)
                         //into newpublisher
                         select new ProductTechnologyView
                         {
                             Id = tech.Id,
                             Number = tech.Number,
                             OrderProductId = tech.OrderProductId,
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             ProductId = pro.Id,
                             //OrderNum = order.Number,
                             Status = tech.Status,
                             ApplyTime = tech.ApplyTime,//本操作开始时间
                             CreateTime = tech.CreateTime,
                             ApplyName = u.RealName
                         };
            return result;

        }


        public IQueryable<ProductTechnologyView> GetListItem(string productName, string productNum, Guid techId)
        {
            var result = from tech in dbcontext.Technologies
                         join orderpro in dbcontext.OrderProducts
                         on tech.OrderProductId equals orderpro.Id
                         join tm in dbcontext.TechnologyManages
                         on orderpro.ProductId equals tm.ProductId
                         join pro in dbcontext.Products
                         on tm.MaterialId equals pro.Id
                         where (pro.Name.Contains(productName))
                          && (pro.Number.Contains(productNum))
                          && (tech.Id == techId)
                         //into newpublisher
                         select new ProductTechnologyView
                         {
                             Id = tech.Id,
                             Number = tech.Number,
                             OrderProductId = tech.OrderProductId,
                             Status = tech.Status,
                             ApplyTime = tech.ApplyTime,//本操作开始时间
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             ProductId = pro.Id,
                             MaterialId = tm.MaterialId,
                         };
            return result;
        }
        public ProductTechnologyView GetFirstItem(Guid techId)
        {
            var result = from tech in dbcontext.Technologies
                         join orderpro in dbcontext.OrderProducts
                         on tech.OrderProductId equals orderpro.Id
                         join tm in dbcontext.TechnologyManages
                         on orderpro.ProductId equals tm.ProductId
                         join pro in dbcontext.Products
                         on tm.MaterialId equals pro.Id
                         where (tech.Id == techId)
                         //into newpublisher
                         select new ProductTechnologyView
                         {
                             Id = tech.Id,
                             Number = tech.Number,
                             OrderProductId = tech.OrderProductId,
                             Status = tech.Status,
                             ApplyTime = tech.ApplyTime,//本操作开始时间
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             ProductId = pro.Id,
                             MaterialId = tm.MaterialId,
                             HelpMemo = tech.HelpMemo,
                             HelpMoney = tech.HelpMoney,
                             SizeWidth = pro.SizeWidth,
                             SizeLenth = pro.SizeLenth,
                             Price = pro.Price,
                             Unit = pro.Unit,
                         };
            return result.FirstOrDefault();
        }

        /// <summary>
        /// 根据关系表 同订产品id对应的不同订单
        /// </summary>
        /// <param name="purchaseId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ProductTechnologyView GetRelationProductList(Guid purchaseId, Guid productId)
        {
            var obj = dbcontext.OrderProducts.FirstOrDefault(a => a.PurchaseId == purchaseId && a.ProductId == productId);
            if (obj != null)
            {
                if (obj.PrimaryID == null)
                {
                    var result = from op in dbcontext.OrderProducts
                                 join order in dbcontext.Orders
                                     on op.OrderId equals order.Id
                                 join pro in dbcontext.Products
                                     on op.ProductId equals pro.Id
                                 join tech in dbcontext.Technologies
                                     on op.Id equals tech.OrderProductId
                                 where op.PurchaseId == purchaseId
                                       && op.ProductId == productId
                                       && op.Status != -1
                                 select new ProductTechnologyView
                                 {
                                     OrderId = order.Id,
                                     OrderNum = order.Number,
                                     OrderProductId = op.Id,
                                     PurchaseId = op.PurchaseId.Value,
                                     CreateTime = order.CreateTime,
                                     ProductName = pro.Name,
                                     ProductNumber = pro.Number,
                                     ProductId = pro.Id,
                                     Amount = op.Amount,
                                     TechnologyId = tech.Id,
                                     SendTime = order.SendTime,
                                     TechType = tech.TechType,
                                     CheckLenth = tech.CheckLenth
                                 };
                    return result.FirstOrDefault();
                }
                else
                {
                    var result = from op in dbcontext.OrderProducts
                                 join order in dbcontext.Orders
                                     on op.OrderId equals order.Id
                                 join pro in dbcontext.Products
                                     on op.ProductId equals pro.Id
                                 join tech in dbcontext.Technologies
                                     on op.PrimaryID equals tech.OrderProductId
                                 where op.PurchaseId == purchaseId
                                       && op.ProductId == productId
                                       && op.Status != -1
                                 select new ProductTechnologyView
                                 {
                                     OrderId = order.Id,
                                     OrderNum = order.Number,
                                     OrderProductId = op.Id,
                                     PurchaseId = op.PurchaseId.Value,
                                     CreateTime = order.CreateTime,
                                     ProductName = pro.Name,
                                     ProductNumber = pro.Number,
                                     ProductId = pro.Id,
                                     Amount = op.Amount,
                                     TechnologyId = tech.Id,
                                     SendTime = order.SendTime
                                 };
                    return result.FirstOrDefault();
                }
            }
            return null;
        }
    }
}
